/**
 * Created by Administrator on 2018/1/25.
 */

    //查看商品分类的子分类
    function show(id){
        $.ajax({
            type : "get",
            url : "queryCategory",
            data : "id="+id,
            dataType:"json",
            success : function(data){
                console.log(data)
                $("#category").html("");
                //如果父类id为零, 就不需要返回按钮
                if(data[0].pid != 0 ){
                    $("#action").html("操作&nbsp;&nbsp;&nbsp;&nbsp; <a  class='layui-btn' onclick='back("+data[0].pid+")'>返回</a>")
                }

                for(var i = 0; i< data.length; i++){
                    var content = '<td>'+data[i].id+'</td>';
                    var content1 = '<td>'+data[i].name+'</td>';
                    var content2 = '<td>'+data[i].pname+'</td>';
                    var content3 = '<td>'+data[i].path+'</td>';
                    if(data[i].is_show == 0){
                        var content4 = "<td><a class='layui-icon' style='font-size: 30px; color: red;' onclick='change_category_show("+data[i].is_show+","+data[i].id+",this)'>&#x1006;</a></td>"
                    }else{
                        var content4 = "<td><a class='layui-icon' style='font-size: 30px; color: #1E9FFF;' onclick='change_category_show("+data[i].is_show+","+data[i].id+",this)'>&#xe618;</a></td>"
                    }
                    //var content4 = '<td>'+data[i].is_show+'</td>';
                    var content5 = '<td>'+data[i].create_time+'</td>';
                    var content6 = '<td>'+data[i].update_time+'</td>';

                    var action = "<a class='layui-btn ' onclick='show("+data[i].id+")'>查看子类</a>";
                    var action1 = '<a href="addCategory?id='+data[i].id+'" class="layui-btn">添加子类</a>';
                    var action2 = '<a href="editCategory?id='+data[i].id+'" class="layui-btn layui-btn-normal">编辑</a>';
                    if(data[i].pid != 0){
                        var action3 = '<a onclick="del('+data[i].id+',this)" class="layui-btn layui-btn-danger">删除</a>';
                    }else{
                        var action3 = '';
                    }
                    var content7 = '<td>'+action+action1+action2+action3+'</td>';

                    $("<tr>").html(content+content1+content2+content3+content4+content5+content6+content7).appendTo($("tbody"));
                }


            }

        })

    }
    //从商品子分类返回父类
    function back(id){
    $.ajax({
        type : "get",
        url : "queryBackCategory",
        data : "id="+id,
        dataType:"json",
        success : function(data){
            console.log(data)
            $("#category").html("");
            ////如果父类id为零, 就不需要返回按钮
            if(data[0].pid != 0 ){
                $("#action").html("操作&nbsp;&nbsp;&nbsp;&nbsp; <a  class='layui-btn' onclick='back("+data[0].pid+")'>返回</a>")
            }else{
                $("#action").html("操作");
            }

            for(var i = 0; i< data.length; i++){
                var content = '<td>'+data[i].id+'</td>';
                var content1 = '<td>'+data[i].name+'</td>';
                var content2 = '<td>'+data[i].pname+'</td>';
                var content3 = '<td>'+data[i].path+'</td>';
                if(data[i].is_show == 0){
                    var content4 = "<td><a class='layui-icon' style='font-size: 30px; color: red;' onclick='change_category_show("+data[i].is_show+","+data[i].id+",this)'>&#x1006;</a></td>"
                }else{
                    var content4 = "<td><a class='layui-icon' style='font-size: 30px; color: #1E9FFF;' onclick='change_category_show("+data[i].is_show+","+data[i].id+",this)'>&#xe618;</a></td>"
                }
                //var content4 = '<td>'+data[i].is_show+'</td>';
                var content5 = '<td>'+data[i].create_time+'</td>';
                var content6 = '<td>'+data[i].update_time+'</td>';

                var action = "<a class='layui-btn ' onclick='show("+data[i].id+")'>查看子类</a>";
                var action1 = '<a href="addCategory?id='+data[i].id+'" class="layui-btn">添加子类</a>';
                var action2 = '<a href="addCategory?id='+data[i].id+'" class="layui-btn layui-btn-normal">编辑</a>';
                if(data[i].pid != 0){
                    var action3 = '<a onclick="del('+data[i].id+',this)" class="layui-btn layui-btn-danger">删除</a>';
                }else{
                    var action3 = '';
                }
                var content7 = '<td>'+action+action1+action2+action3+'</td>';

                $("<tr>").html(content+content1+content2+content3+content4+content5+content6+content7).appendTo($("tbody"));
            }


        }

    })

}

    //删除子分类
    function del(id,obj){
        $.ajax({
            type : "get",
            url : "delCategory",
            data : "id="+id,
            success : function(data){
                if(data == -1){
                    alert("该分类下有子分类存在,不能删除");
                }else if(data == -2){
                    alert("该分类下有商品存在,不能删除");
                }else{
                    $(obj).parent().parent().remove();
                }
            }
        })
    }









